﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using weikebuluo.DAL;
using weikebuluo.Model;

namespace weikeweixin.web.admin.website
{
    public partial class newsadd : weikeweixin.web.admin.cs.adminbase
    {
        t_news_dal NewsDAL = new t_news_dal();
        t_news_model NewsModel = new t_news_model();

        protected string CId = "";
        protected string PicUrl = "";
        protected string nPicUrl = "/web/admin/website/styles/images/home-default21.jpg";
        protected string Status = "";
        protected string Name = "";
        protected string Sort = "0";
        protected string Type = "link";
        protected string KeyWords = "";
        protected string Description = "";
        protected string PageTitle = "添加图文";
        protected string Content = "";
        protected string Stick = "";
        protected string NCreateDate = "";
        protected string NContentType = "";
        protected string NStartDate = DateTime.Now.ToString();
        protected string NEndDate = DateTime.Now.AddDays(6).ToString();
        protected string NIndustryType = "";
        protected string NIndustryName = "";
        protected string NProvince = "";
        protected string NCity = "";
        protected string NArea = "";
        protected string NDnotTitle = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && Request.QueryString["id"] != null)
            {
                DataTable dtNews = NewsDAL.GetList("NId=" + Request.QueryString["id"]).Tables[0];

                PageTitle = " 修改图文";

                if (dtNews.Rows.Count > 0)
                {
                    CId = dtNews.Rows[0]["CId"].ToString();
                    PicUrl = dtNews.Rows[0]["NSPicUrl"].ToString();
                    nPicUrl = dtNews.Rows[0]["NPicUrl"].ToString();
                    Status = dtNews.Rows[0]["NStatus"].ToString();
                    Name = dtNews.Rows[0]["NTitle"].ToString();
                    KeyWords = dtNews.Rows[0]["KeyWords"].ToString();
                    Description = dtNews.Rows[0]["Description"].ToString();
                    Content = dtNews.Rows[0]["NContent"].ToString();
                    Sort = dtNews.Rows[0]["NSort"].ToString();
                    Stick = dtNews.Rows[0]["Stick"].ToString();
                    NCreateDate = dtNews.Rows[0]["NCreateDate"].ToString();
                    NContentType = dtNews.Rows[0]["NContentType"].ToString();
                    NStartDate = dtNews.Rows[0]["NStartDate"].ToString();
                    NEndDate = dtNews.Rows[0]["NEndDate"].ToString();
                    NIndustryType = dtNews.Rows[0]["NIndustryType"].ToString();
                    NIndustryName = dtNews.Rows[0]["NIndustryName"].ToString();
                    NProvince = dtNews.Rows[0]["NProvince"].ToString();
                    NCity = dtNews.Rows[0]["NCity"].ToString();
                    NArea = dtNews.Rows[0]["NArea"].ToString();
                    NDnotTitle = dtNews.Rows[0]["NDnotTitle"].ToString();
                }
            }

            switch (Request.Form["action"])
            {
                case "save":
                    Save();
                    break;
            }
        }

        private void Save()
        {
            NewsModel.Description = Request.Form["description"];
            NewsModel.KeyWords = Request.Form["KeyWords"];
            NewsModel.user_id = int.Parse(user_id);
            NewsModel.NSPicUrl = Request.Form["icon"];
            NewsModel.NPicUrl = Request.Form["picurl"];
            NewsModel.NStatus = int.Parse(string.IsNullOrEmpty(Request.Form["is_home"]) ? "1" : Request.Form["is_home"]);
            NewsModel.NTitle = Request.Form["name"];
            NewsModel.Stick = int.Parse(string.IsNullOrEmpty(Request.Form["Stick"]) ? "0" : Request.Form["Stick"]);
            NewsModel.NSort = int.Parse(string.IsNullOrEmpty(Request.Form["sort"]) ? "0" : Request.Form["sort"]);
            NewsModel.NProvince = Request.Form["location_p"].Trim();
            NewsModel.NCity = Request.Form["location_c"].Trim();
            NewsModel.NArea = Request.Form["location_a"].Trim();
            NewsModel.NIndustryType = Request.Form["category_f"].Trim();
            NewsModel.NIndustryName = Request.Form["category_s"].Trim();
            string[] NTime = Request.Form["time"].Split('-');
            NewsModel.NStartDate = DateTime.Parse(NTime[0].Trim());
            NewsModel.NEndDate = DateTime.Parse(NTime[1].Trim());
            NewsModel.NDnotTitle = Convert.ToInt32(Request.Form["NDnotTitle"] == "on" ? "1" : "0");

            if (string.IsNullOrEmpty(Request.Form["category_id"]))
            {
                NewsModel.CId = null;
            }
            else
            {
                NewsModel.CId = int.Parse(Request.Form["category_id"]);
            }

            NewsModel.NContentType = Request.Form["Type"];

            switch (NewsModel.NContentType)
            {
                case "Content":
                    NewsModel.NContent = Request.Form["content"];
                    break;
                case "Href":
                    NewsModel.NContent = Request.Form["Href"];
                    break;
            }

            if (string.IsNullOrEmpty(Request.Form["id"]))
            {
                NewsModel.NCreateDate = DateTime.Now;

                int re = NewsDAL.Add(NewsModel);
                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"news.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"news.aspx\",\"error\":\"\"}");
                }
            }
            else
            {
                NewsModel.NId = int.Parse(Request.Form["id"]);
                NewsModel.NCreateDate = DateTime.Parse(Request.Form["NCreateDate"]);

                bool re = NewsDAL.Update(NewsModel);

                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"news.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"news.aspx\",\"error\":\"\"}");
                }
            }
            Response.End();
        }
    }
}